package com.drakulo.games.ais.ui.component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

import com.drakulo.games.ais.core.Colony;
import com.drakulo.games.ais.core.Resource;
import com.drakulo.games.ais.core.delayed.ResearchAction;
import com.drakulo.games.ais.ui.FontHelper;
import com.drakulo.games.ais.ui.ImageManager;
import com.drakulo.games.ais.ui.UIHelper;
import com.drakulo.games.ais.ui.twlbridge.RootPane;

import de.matthiasmann.twl.Label;

/**
 * UIComponent used in the Planet vision. Displays a colony summary
 * 
 * @author Drakulo
 * 
 */
public class ColonySelector extends UIComponent {
	/** The Component fixed width */
	public static final int WIDTH = 300;
	/** The component fixed height */
	public static final int HEIGHT = 100;

	/** The linked colony */
	private Colony colony;
	/** The resource labels */
	private Map<Resource, Label> labels;
	/** The research progress bar */
	private ProgressBar researchPB;
	/** Select flag */
	private boolean selected;

	public ColonySelector(final Colony colony, RootPane twlRootPane) {
		this.colony = colony;
		this.setHeight(HEIGHT);
		this.setWidth(WIDTH);
		this.labels = new HashMap<Resource, Label>();
		for (Resource r : Resource.values()) {
			Label l = new Label();
			l.setTheme("resourceLabel");
			l.setSize(80, 15);
			twlRootPane.add(l);
			this.labels.put(r, l);
		}

		this.researchPB = new ProgressBar();
		this.researchPB.setHeight(10);
		this.researchPB.setWidth(167);
		this.researchPB.setEmptyColor(Color.darkGray);
		this.researchPB.setFilledColor(Color.magenta);
		
	}

	@Override
	public void render(Graphics g) throws SlickException {
		// The hover border
		if(this.isHovered()){
			g.setColor(Color.yellow);
		}
		if(this.selected){
			g.setColor(Color.green);
		}
		if(this.hovered || this.selected){
			g.drawRect(this.getOX()-1, this.getOY() - 1, WIDTH+2, HEIGHT+2);
		}
		
		// Render the background box
		UIHelper.drawBox(g, this.getOX(), this.getOY(), WIDTH, HEIGHT);

		final Font bigFont = FontHelper.getFont(FontHelper.HEMI_HEAD,
				Color.white, 20);
		final Font smallFont = FontHelper.getFont(FontHelper.HEMI_HEAD,
				Color.white, 15);
		final int dpadding = 4;

		int x = this.getOX() + 2;
		int y = this.getOY() + 2;

		// Colony info background
		UIHelper.drawDarkBox(g, x, y, 175, 40);

		// Colony name
		bigFont.drawString(x + 2, y + 2, this.colony.getName());

		// The planet name
		y += 20;
		smallFont.drawString(x + 12, y + 2, this.colony.getPlanetName());

		UIHelper.drawDarkBox(g, x, y + 35, 175, 40);

		ResearchAction research = this.colony.getResearch();
		if (research != null) {
			// The colony's current research name
			smallFont.drawString(x + 4, y + 45, research.getTechnology()
					.getName());

			// The colony's current research progress
			this.researchPB.setMaxValue(BigDecimal.valueOf(research
					.getDuration()));
			this.researchPB.setValue(BigDecimal.valueOf(research
					.getCurrentValue()));
			this.researchPB.setOX(x + 4);
			this.researchPB.setOY(this.getOY() + 2 + HEIGHT - dpadding - 10
					- dpadding);
			researchPB.render(g);
		}

		// Resources
		final int width = 114;
		final int height = 101;
		x = getWidth() - width;
		y = getOY();
		UIHelper.drawDarkBox(g, x += 3, y += 3, width, height - 6);

		Resource[] resources = Resource.values();
		Image img;
		x += 3;
		Map<Resource, BigDecimal> resourceMap = this.colony.getResources();
		for (Resource r : resources) {
			UIHelper.drawDarkBox(g, x, y += 3, 24, 20);
			img = ImageManager.getGfx(r.getGfxBase() + "-small");
			img.draw(x + 3, y, r.getColor());

			String value = resourceMap.get(r).toString();
			Label lbl = this.labels.get(r);

			final int zoneHeight = 15;
			UIHelper.drawDarkBox(g, x + 24, y, lbl.getWidth() + 4, zoneHeight);
			lbl.setPosition(x + 24, y);
			lbl.setText(value);
			lbl.setVisible(true);

			y += 20;
		}

	}

	@Override
	public void update(Input input) throws SlickException {
		this.updateHovered(input.getMouseX(), input.getMouseY());
	}

	public Colony getColony() {
		return this.colony;
	}
	
	public void select(boolean selected) {
		this.selected = selected;
	}
}
